﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace BlankApp1.ViewModels
{
    class UserControl2ViewModel: BindableBase
    {
        // 定义命令属性
        public DelegateCommand SaveCommand { get; }
        public DelegateCommand<string> DeleteCommand { get; } // 带参数的命令

        public ObservableCollection<string> Items { get; } = new ObservableCollection<string>();

        public UserControl2ViewModel()
        {
            // 初始化命令，传入执行方法
            SaveCommand = new DelegateCommand(OnSave);

            // 带条件判断的命令（CanExecute方法）
            DeleteCommand = new DelegateCommand<string>(OnDelete)
                .ObservesCanExecute(() => CanDelete);

            Items.Add("Item 1");
            Items.Add("Item 2");
            Items.Add("Item 3");
            Items.Add("Item 4");
        }

        private void OnSave()
        {
            // 保存逻辑
            MessageBox.Show("保存逻辑");
        }

        private void OnDelete(string id)
        {
            // 删除指定ID的项
            MessageBox.Show("删除指定ID的项");
        }

        private bool CanDelete => SelectedItem != null;
        private object _selectedItem;
        public object SelectedItem
        {
            get => _selectedItem;
            set
            {
                SetProperty(ref _selectedItem, value);
                DeleteCommand.RaiseCanExecuteChanged();
            }
        }
    }
}
